package com.briup.estore.listener;

import com.briup.estore.entity.BannerEntity;
import com.briup.estore.entity.vo.CategoryVO;
import com.briup.estore.service.BannerService;
import com.briup.estore.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @author adam
 * @date 2023/3/31
 * servletContextListener 监听application的生命周期
 * 应用创建的时候 查询分类信息 将分类信息放到application
 */

@Slf4j
public class ApplicationListener implements ServletContextListener {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BannerService bannerService;

    @Value("${nginx.path}")
    private  String path;


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        List<CategoryVO> list = categoryService.findAllCategory();
        List<BannerEntity> banners = bannerService.findActive();
        context.setAttribute("categories",list);
        context.setAttribute("banners",banners);
        context.setAttribute("path",path);

    }
}
